﻿using System;
using GDIT.BLL;
using GDIT.Model;
using GDIT.Base.PageBaseHelper;
using DevExpress.Xpo;
using System.Data;

namespace GDIT.page.manage.link
{
    public partial class guideLink1 : System.Web.UI.Page
    {

        private BMainMenus main = null;
        private BDetailMenus detail = null;
        private Mtb_MainMenus menu = null;
        private Mtb_DetailMenus detailMenu = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Session["UserName"].ToString()))
            {
                PageBase.MessageBoxShowAndGo("检查到你还没有登录，请先登录，谢谢！", "~/page/manage/login.aspx", Page);
                return;
            }
            if (!IsPostBack)
            {
                BindMain();
                BindDetail();
            }
        }

        //绑定主菜单
        private void BindMain()
        {
            main = new BMainMenus();
            this.MainNavRepeater.DataSource = main.SelectMainMenus();
            this.MainNavRepeater.DataBind();
        }

        //绑定详细菜单
        private void BindDetail()
        {
            detail = new BDetailMenus();
            IDataReader xp = detail.SelectDetailMenus();
            this.DetailNavRepeater.DataSource = xp;
            this.DetailNavRepeater.DataBind();
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            /*提示*/
            string[] id = txtId.Text.Trim().Split('_');//链接id
            try
            {
                if (id[0] == "0")//则说明该ID菜单栏目是主菜单
                {
                    main = new BMainMenus();
                    menu = new Mtb_MainMenus();
                    menu.OID = Convert.ToInt32(id[1]);
                    menu.MenuName = txtName.Text.Trim();
                    menu.LinkAddress = txtLink.Text.Trim();
                    bool result = main.UpdateMainMenus(menu);
                    if (!result)
                        PageBase.MessageBoxShow("更新失败！", Page);
                    else
                        PageBase.MessageBoxShow("更新成功！", Page);
                }
                else
                {
                    detailMenu = new Mtb_DetailMenus();
                    detailMenu.OID = Convert.ToInt32(id[1]); ;
                    detailMenu.MenuName = txtName.Text.Trim();
                    detailMenu.LinkAddress = txtLink.Text.Trim();
                    detail = new BDetailMenus();
                    bool result = detail.UpdateDetailMenus(detailMenu);
                    if (!result)
                        PageBase.MessageBoxShow("更新失败！", Page);
                    else
                        PageBase.MessageBoxShow("更新成功！", Page);
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作错误，原因："+ex.Message.ToString(),Page); ;
            } 
        }

        protected void btnAdd_OnClick(object sender, EventArgs e)
        {
            string[] id = txtId.Text.Trim().Split('_');//链接id
            try
            {
                if (id[0] == "0")//则说明该ID菜单栏目是主菜单
                {
                    main = new BMainMenus();
                    menu = new Mtb_MainMenus();
                    menu.MenuName = txtName.Text.Trim();
                    menu.LinkAddress = txtLink.Text.Trim();
                    bool result = main.InsertMainMenus(menu);
                    if (!result)
                        PageBase.MessageBoxShow("添加失败！", Page);
                    else
                        PageBase.MessageBoxShow("添加成功！", Page);
                }
                else
                {;
                    detailMenu = new Mtb_DetailMenus();
                    detail = new BDetailMenus();
                    detailMenu.MenuName = txtName.Text.Trim();
                    detailMenu.LinkAddress = txtLink.Text.Trim();
                    menu = new Mtb_MainMenus();
                    menu.OID = Convert.ToInt32(id[0]);
                    menu.Details.Add(detailMenu);
                    bool result = detail.InsertDetailMenus(menu);
                    if (!result)
                        PageBase.MessageBoxShow("添加失败！", Page);
                    else
                        PageBase.MessageBoxShow("添加成功！", Page);
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作错误，原因："+ex.Message.ToString(),Page); 
            }
        }

        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            string[] id = txtId.Text.Trim().Split('_');//链接id
            try
            {
                if (id[0] == "0")//则说明该ID菜单栏目是主菜单
                {
                    main = new BMainMenus();
                    menu = new Mtb_MainMenus();
                    menu.OID = Convert.ToInt32(id[1]);
                    menu.MenuName = txtName.Text.Trim();
                    menu.LinkAddress = txtLink.Text.Trim();
                    bool result = main.DeleteMainMenus(menu);
                    if (!result)
                        PageBase.MessageBoxShow("删除失败！", Page);
                    else
                        PageBase.MessageBoxShow("删除成功！", Page);
                }
                else
                {
                    detailMenu = new Mtb_DetailMenus();
                    detail = new BDetailMenus();
                    detailMenu.OID = Convert.ToInt32(id[1]);
                    detailMenu.MenuName = txtName.Text.Trim();
                    detailMenu.LinkAddress = txtLink.Text.Trim();
                    bool result = detail.DeleteDetailMenus(detailMenu);
                    if (!result)
                        PageBase.MessageBoxShow("删除失败！", Page);
                    else
                        PageBase.MessageBoxShow("删除成功！", Page);
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作错误，原因："+ex.Message.ToString(),Page); 
            }
        }

    }
}